package com.wanxi.servlet.cuisine;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.CuisineModel;
import com.wanxi.service.CuisineService;
import com.wanxi.service.impl.CuisineServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/cuisine/add")
public class CuisineAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//        2.得到前端的值
        String name=req.getParameter("name");
//        3.封装到model
        CuisineModel model= CuisineModel.builder()
                .name(name)
                .build();
//        4.调用服务逻辑层
        CuisineService cuisineServiceImpl =new CuisineServiceImpl();
        String result= cuisineServiceImpl.add(model);
//        5.响应
        resp.getWriter().println(JSONObject.toJSONString(ResultDto.getInstance(result)));
    }
}
